package com.sfy.mallchat.common.common.thread;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ThreadFactory;

@AllArgsConstructor
@Slf4j
public class MyThreadFactory implements ThreadFactory {

    private static final MyUncaughtExceptionHandler myUncaughtExceptionHandler
            = new MyUncaughtExceptionHandler();
    private ThreadFactory original; // 用到装饰器模式,进行复用,实现线程的异常捕获
    @Override
    public Thread newThread(Runnable r) {
        Thread thread = original.newThread(r);
        thread.setUncaughtExceptionHandler(myUncaughtExceptionHandler);
        return thread;
    }
}
